// 一些公用函数

/**
 * 获取url中的id参数 url: /user/edit/{id}
 * 
 * @returns
 */
function getIdFromUrl() {
	var addr = location.href;
	var index = addr.lastIndexOf('/') + 1;
	var id = addr.substr(index);
	return id;
}
/**
 * 获取URL中的参数
 * @param name
 * @returns
 */
function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
} 
/**
 * 关闭layer打开的窗口
 */
function closeWin() {
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}

/**
 * 重置表单,id参数为空重置页面第一个表单
 */
function resetForm(id) {
	if (id) {
		$('#' + id)[0].reset();
	} else {
		document.forms[0].reset();
	}

}

function emptyempty (mixed_var) {  
    var key;  
   
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || typeof mixed_var === 'undefined') {  
        return true;  
    }  
   
    if (typeof mixed_var == 'object') {  
        for (key in mixed_var) {  
            return false;  
        }  
        return true;  
    }  
   
    return false;  
}

/**
 * 全反选
 */
function checkAll(value){
	$('input[name="ids"]').attr("checked",value); 
}
/**
 * 替换
 * @param s1
 * @param s2
 * @returns
 */
String.prototype.replaceAll = function(s1,s2){ 
	return this.replace(new RegExp(s1,"gm"),s2); 
}
/**
 * 刷新父窗口表格
 */
function refreshParentList() {
	parent.app.query(parent.app.pageData.currentPage);
}
/**
 * 刷新用户、用户组、权限表格
 */
function refreshUParentList() {
	parent.query(parent.app.pageData.currentPage);
}

/**
 * session判断
 * @param data
 */
function sessionCheckTimeOut(data)
{
	if (data && typeof data=="string" && data.indexOf("<html")>0) {
		layer.alert("系统登录超时，请退出重新登录！");
	}else{
		layer.alert(data.message);
	}
}

//过滤设备屏幕类型 0 横屏 1 竖屏
/*Vue.filter('deviceTypeFilter', function (value) {
    if(value == 0){
        return "横屏";
    } else if(value == 1){
    	return "竖屏";
    }
});*/

//过滤设备审核状态 ：待审核pendingApproval,审核不通过referApproval,审核通过passApproval
Vue.filter('deviceStateFilter', function (value) {
	if(value == "pendingApproval"){
    	return "待审核";
    }else if(value == "referApproval"){
    	return "审核拒绝";
    }else if(value == "passApproval"){
    	return "审核通过";
    }
});
//过滤广告投放记录状态
Vue.filter('deviceAdsStatusFilter', function (value) {
	if(value == "0"){
    	return "有效";
    }else{
    	return "失效";
    }
});
//在线online,离线offline,
Vue.filter('deviceIMStateFilter', function (value) {
    if(value == "Online"){
        return "在线";
    } else if(value == "Offline"){
    	return "离线";
    }else if(value == "PushOnline"){
    	return "断开";
    }
});
//max大图 ，med中图，min小图
Vue.filter('cultureTitleTypeFilter', function (value) {
    if(value == "max"){
        return "大图";
    } else if(value == "med"){
    	return "中图";
    }else if(value == "min"){
    	return "小图";
    }
});
//是否过滤器 1 是 0 否
Vue.filter('recommendFilter', function (value) {
    if(value == 1){
        return "一级推荐";
    } else if(value==2){
    	return "二级推荐";
    }else if(value==3){
    	return "三级推荐";
    }
});

//过滤设备屏幕类型 0 横屏 1 竖屏
Vue.filter('userInfoSex', function (value) {
	if(value == 1){
		return "男";
	} else if(value == 2){
		return "女";
	}
});

//是否过滤器 1 是 0 否
Vue.filter('productTypeFilter', function (value) {
    if(value == "outimport"){
        return "外部导入";
    } else{
    	return "平台新增";
    }
});

//过滤珠宝旅游广告的跳转类型
Vue.filter('adsTypeFilter', function (value) {
    if(value == "0"){
        return "图片";
    } else if(value == "1"){
    	return "视频";
    }else if(value == "2"){
    	return "分类";
    }else if(value == "3"){
    	return "商品";
    }else if(value == "4"){
    	return "优惠券";
    }
});

//用户反馈及建议的状态
Vue.filter('feedBackState', function (value) {
    if(value == "read"){
        return "已读";
    } else if(value == "unread"){
    	return "未读";
    }
});

//app系统类型
Vue.filter('appOsTypeFilter', function (value) {
    if(value == 1){
        return "ios";
    } else{
    	return "android";
    }
});
Vue.filter('appIsNewFilter', function (value) {
    if(value == 1){
        return "是";
    } else{
    	return "否";
    }
});

Vue.filter('deviceTypeFilter', function (value) {
	if(value == 0){
		return "横屏";
	} else if(value == 1){
		return "竖屏";
	}else{
		return "待设置";
	}
});
//商品是否导入
Vue.filter('productImportStatusFilter', function (value) {
	if(value == true){
		return "已导入";
	} else{
		return "未导入";
	}
});
//商品是否导入
Vue.filter('marketClassifyFilter', function (value) {
	if(value == 0){
		return "普通应用";
	} else{
		return "行业应用";
	}
});
//商品是否导入
Vue.filter('adsClassifyFilter', function (value) {
	if(value == 0){
		return "广投墙广告分类";
	} else{
		return "新零售广告分类";
	}
});
//推荐类型
Vue.filter('recommendTypeFilter', function (value) {
    if(value == 1){
        return "注册";
    } else{
    	return "开卡";
    }
});

//优惠卷状态
Vue.filter('discountStatusFilter', function (value) {
	if(value == 1){
        return "上架";
    } else if(value==2){
    	return "下架";
    }else if(value==3){
    	return "删除";
    }else if(value==0){
    	return "初始";
    }
});

//领取优惠卷状态
Vue.filter('discountOrderStatusFilter', function (value) {
	if(value == 0){
        return "未使用";
    } else if(value==1){
    	return "已使用";
    }else if(value==2){
    	return "已失效";
    }else if(value==3){
    	return "已删除";
    }
});


Vue.filter('diffStateFilter', function (value) {
	  if(value == 10){
	      return "一致";
	  } else {
	  	return "不一致";
	  }
	});


Vue.filter('appTemplateType', function (value) {
	if(value == "0"){
		return "广告模板";
	} else if(value == "1"){
		return "新零售模板";
	}else if(value == "2"){
		return "其他模板";
	}
});

Vue.filter('appTemplateStatus', function (value) {
	if(value == "0"){
		return "停用";
	} else if(value == "1"){
		return "启用";
	}
});

Vue.filter('ishot', function (value) {
	if(value == "0"){
		return "否";
	} else if(value == "1"){
		return "是";
	}
});

	

function getSelectedIds() {
	var selectdItems = $(":checkbox[name=ids]:checked");
	var ids = [];
	selectdItems.each(function() {
		ids.push($(this).val());
	});
	return ids;
}



Vue.filter('shopNameFilter', function (value) {
	var data=null;
	var tip = layer.load(0, {
		shade : false
	});
	$.ajax({
		type : "POST",
		async:false,
		url : '/baseinfo/storeInfo/queryPage',
		data : {
			pageNo : 1,
			pageSize : 500,
			search_LIKE_sapynumod :value
		},
		beforeSend : function() {
		},
		complete : function() {
			layer.close(tip);
		},
		error : function(request) {
			layer.alert("系统错误");
		},
		success : function(result) {
			if (result.status == 0) {
			 data = result.data;												
			} else {
				layer.alert(result.message);
			}
			}
		});
		if(data.rows[0]!=null){
		return data.rows[0].shopname;
		}
	});


Vue.filter('sapshopidFilter', function (value) {
	var data=null;
	var tip = layer.load(0, {
		shade : false
	});
	$.ajax({
		type : "POST",
		async:false,
		url : '/baseinfo/storeInfo/queryPage',
		data : {
			pageNo : 1,
			pageSize : 500,
			search_LIKE_shopid :value
		},
		beforeSend : function() {
		},
		complete : function() {
			layer.close(tip);
		},
		error : function(request) {
			layer.alert("系统错误");
		},
		success : function(result) {
			if (result.status == 0) {
			 data = result.data;												
			} else {
				layer.alert(result.message);
			}
			}
		});
		if(data.rows[0]!=null){
		return data.rows[0].sapynumod;
		}
	});

function layer_full(title,url,w,h){
	var index = layer.open({
		type: 2,
		shade:0.4,
		title: title,
		content: url
	});
	layer.full(index);
}

function downloadFromUrl(url) {
	var downloadFrame = $("#downloadFrame");
	if (!downloadFrame[0]) {
		$(document.body).append("<iframe id='downloadFrame' style='width:0px; height:0px'></iframe>");
	}
	$("#downloadFrame").prop("src", url);
}

function obj2FormDataStr(data) {
	var params = [];
	for(var p in data) {
		params.push(p + "=" + data[p]);
	}
	return encodeURI(params.join("&"))
}

Vue.filter('toThousands', function (num) {
    var num = (num || 0).toString(), result = '';
    while (num.length > 3) {
        result = ',' + num.slice(-3) + result;
        num = num.slice(0, num.length - 3);
    }
    if (num) { result = num + result; }
    return result;
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}